<?php

declare(strict_types=1);

namespace Hyperfx\Framework\Exception;

use Hyperf\Server\Exception\ServerException;
use Hyperfx\Framework\Constants\ErrorCode;
use Throwable;

class AppException extends ServerException
{
    /**
     * 上下文信息.
     *
     * @var array
     */
    protected $context = [];

    private bool $debug = true;

    public function __construct(int $code, string $message = null, array $context = [], Throwable $previous = null)
    {
        $this->context = $context;

        if (is_null($message)) {
            $message = ErrorCode::getMessage($code);
        }

        parent::__construct($message, $code, $previous);
    }

    /**
     * @return array
     */
    public function getContext()
    {
        return $this->context;
    }

    /**
     * @return bool
     */
    public function isDebug(): bool
    {
        return $this->debug;
    }

    /**
     * @param bool $debug
     */
    public function setDebug(bool $debug): void
    {
        $this->debug = $debug;
    }
}